package myJs.implicits

import myJs.myPkg.RSelect.{SelectOption}
import myJs.myPkg.reactHookForm.ErrorField

import scala.scalajs.js
import scala.scalajs.js.|
import myJs.Implicits._

/**
 * Created by Administrator on 2020/1/16
 */
trait MyErrorFieldTool {

  implicit class MyErrorField(v: js.Dictionary[ErrorField]) {

    def deepGet(name: String) = {
      val names = name.split("\\.").toList
      names.size match {
        case x if x == 1 =>
          v(name)
        case x if x == 3 =>
          val curI = names(1).toInt
          val array = v(names(0)).asInstanceOf[js.Array[js.Dictionary[ErrorField]]]
          array(curI)(names(2))
      }
    }

    def deepDefinedAt(name: String) = {
      val names = name.split("\\.").toList
      names.size match {
        case x if x == 1 =>
          v.isDefinedAt(name)
        case x if x == 3 =>
          val curI = names(1).toInt
          val op = v.get(names(0)).flatMap { y =>
            val array = y.asInstanceOf[js.Array[js.UndefOr[js.Dictionary[ErrorField]]]]
            array.toList.get(curI)
          }.flatMap { y =>
            y.toOption
          }.flatMap { y =>
            y.get(names(2))
          }
          op.isDefined
      }
    }


  }

}
